package com.zbl.controller;

import com.google.common.collect.Maps;
import com.zbl.dto.LoginParam;
import com.zbl.parser.CourseChapterJsoupParse;
import com.zbl.parser.CourseJsoupParse;
import com.zbl.service.JsonCrawlerService;
import com.zbl.utils.ImageUtils;
import com.zbl.vo.WrapperRequestParam;
import org.apache.commons.io.IOUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.*;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: zhangjun
 * @Date: 2020/7/30 10:51
 * @Description: 采集非Web页面，如JSON接口等，直接输出响应数据
 */
@RestController
@RequestMapping("/api")
public class JsonCrawlerController {
    @Resource
    private JsonCrawlerService crawlerService;

    /**
     * 模拟登陆，获取用户cookie信息
     * @param loginParam
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    @PostMapping("/login")
    public String login(@Validated @RequestBody LoginParam loginParam) throws IOException, InterruptedException {
        return crawlerService.loginSimulation(loginParam);
    }

    /**
     * 爬取登录成功后个人中心信息
     * @return
     */
    @RequestMapping("/crawlerUserInfo")
    public String crawlerUserInfo() {
        WrapperRequestParam param = new WrapperRequestParam();

        param.setUrl("https://www.quanben.net/user.php");
        crawlerService.crawlerUserInfo(param);
        return "ok";
    }

    /**
     * 爬取慕课网 课程列表信息
     * @return
     */
    @RequestMapping("/crawler")
    public String crawler() {
        WrapperRequestParam param = new WrapperRequestParam();

        Map headers = Maps.newHashMap();
        headers.put("Cookie","imooc_uuid=64fa2fb6-2e47-444d-b5b2-c2e6fb91e515; imooc_isnew_ct=1584411433; zg_did=%7B%22did%22%3A%20%22170e647e404a30-0d38290306ba49-4313f6a-1fa400-170e647e405c72%22%7D; imooc_isnew=2; UM_distinctid=1713471c7149a3-06cca96d7a1813-4313f6a-1fa400-1713471c7159ed; zg_f375fe2f71e542a4b890d9a620f9fb32=%7B%22sid%22%3A%201587460314155%2C%22updated%22%3A%201587460314494%2C%22info%22%3A%201587460314157%2C%22superProperty%22%3A%20%22%7B%5C%22%E5%BA%94%E7%94%A8%E5%90%8D%E7%A7%B0%5C%22%3A%20%5C%22%E6%85%95%E8%AF%BE%E7%BD%91%E6%95%B0%E6%8D%AE%E7%BB%9F%E8%AE%A1%5C%22%2C%5C%22Platform%5C%22%3A%20%5C%22web%5C%22%7D%22%2C%22platform%22%3A%20%22%7B%7D%22%2C%22utm%22%3A%20%22%7B%7D%22%2C%22referrerDomain%22%3A%20%22www.baidu.com%22%2C%22zs%22%3A%200%2C%22sc%22%3A%200%2C%22firstScreen%22%3A%201587460314155%7D; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%227156571%22%2C%22first_id%22%3A%2217210c538b84a7-0a0bb42d6a561b-4313f6a-2073600-17210c538b9921%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E8%87%AA%E7%84%B6%E6%90%9C%E7%B4%A2%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fwww.baidu.com%2Flink%22%7D%2C%22%24device_id%22%3A%2217210c538b84a7-0a0bb42d6a561b-4313f6a-2073600-17210c538b9921%22%7D; IMCDNS=0; Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1596079147; PSEID=7cb271263b8d122d95589601ab2ab4f1; loginstate=1; apsid=I1NDBkNDhlMTk3NGNhODQ3NjViY2Y2MTZiYTYxMjEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANzE1NjU3MQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGQwOWRkY2RkMTQyZDhhOWM5YjI2ZjdlZTQ2YjY5NmY4yjwiX648Il8%3DZW; Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1596079316; cvde=5f223c29d6591-11");
        param.setHeaderMap(headers);

        param.setUrl("https://www.imooc.com/u/index/allcourses");
        crawlerService.crawler(param, new CourseJsoupParse());
        return "ok";
    }

    @RequestMapping("/courseChapter")
    public String courseChapter() {
        WrapperRequestParam param = new WrapperRequestParam();

        Map headers = Maps.newHashMap();
        headers.put("Cookie","imooc_uuid=64fa2fb6-2e47-444d-b5b2-c2e6fb91e515; imooc_isnew_ct=1584411433; zg_did=%7B%22did%22%3A%20%22170e647e404a30-0d38290306ba49-4313f6a-1fa400-170e647e405c72%22%7D; imooc_isnew=2; UM_distinctid=1713471c7149a3-06cca96d7a1813-4313f6a-1fa400-1713471c7159ed; zg_f375fe2f71e542a4b890d9a620f9fb32=%7B%22sid%22%3A%201587460314155%2C%22updated%22%3A%201587460314494%2C%22info%22%3A%201587460314157%2C%22superProperty%22%3A%20%22%7B%5C%22%E5%BA%94%E7%94%A8%E5%90%8D%E7%A7%B0%5C%22%3A%20%5C%22%E6%85%95%E8%AF%BE%E7%BD%91%E6%95%B0%E6%8D%AE%E7%BB%9F%E8%AE%A1%5C%22%2C%5C%22Platform%5C%22%3A%20%5C%22web%5C%22%7D%22%2C%22platform%22%3A%20%22%7B%7D%22%2C%22utm%22%3A%20%22%7B%7D%22%2C%22referrerDomain%22%3A%20%22www.baidu.com%22%2C%22zs%22%3A%200%2C%22sc%22%3A%200%2C%22firstScreen%22%3A%201587460314155%7D; IMCDNS=0; PSEID=7cb271263b8d122d95589601ab2ab4f1; isShowwechat=1; Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1596079147,1596089748,1596089796,1596089809; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%227156571%22%2C%22first_id%22%3A%2217210c538b84a7-0a0bb42d6a561b-4313f6a-2073600-17210c538b9921%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%2217210c538b84a7-0a0bb42d6a561b-4313f6a-2073600-17210c538b9921%22%7D; loginstate=1; apsid=I1NDBkNDhlMTk3NGNhODQ3NjViY2Y2MTZiYTYxMjEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAANzE1NjU3MQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGViM2IzYTE3ZmMxMDg5NjAwY2ZlYTdlZjJjNWY4NTRk8q4jX648Il8%3DZW; Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1596174703; cvde=5f223c29d6591-91");
        param.setHeaderMap(headers);

        param.setUrl("https://www.imooc.com/learn/85");
        crawlerService.crawler(param, new CourseChapterJsoupParse());
        return "ok";
    }

    @RequestMapping("/updateChapter")
    public String updateChapter() {
        WrapperRequestParam param = new WrapperRequestParam();
        crawlerService.updateChapter(param);
        return "ok";
    }

    /**
     * 获取登录验证码
     * @param loginParam
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/getCheckCodeImage")
    public String getCheckCodeImage(@RequestBody LoginParam loginParam) throws IOException {
        InputStream inputStream = null;
        OutputStream output = null;
        try {
            inputStream = crawlerService.getCheckCodeImage(loginParam);
            output = new FileOutputStream(new File("D:/2.jpg"));
            IOUtils.copy(inputStream, output);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (output != null) {
                output.close();
            }
            output.flush();
        }
        return ImageUtils.getImgContent("D:/2.jpg");
    }
}
